const util = {
	
	// 查找匹配的聊天
	findChatIndex(chatList,msg,sign) {
		let sender=msg.sender;
		if("ANONYMOUS_MSG"===sign){
			sender= 'anonymous_'+msg.sender
		}
		return chatList.findIndex(chat => 
		  chat.id === sender
		);
	},
	// 更新现有聊天
	   updateExistingChat(chatList,index, addr,newMsg,sign) {
	     const chat = chatList[index]
	     console.log("正在跟新",sign)
	     // 更新聊天信息
	     chat.name = newMsg.name || chat.name
	     chat.avatar = addr+newMsg.avatar_path || chat.avatar
	     chat.readed=true;
		if(newMsg.auth==="1"){
			chat.roleType=1
		}else if(newMsg.auth==="2"){
			chat.roleType=2
		}else if(newMsg.auth==="3"){
			chat.roleType=3
		}else{
			chat.roleType=0
		}
					  
	     // 添加新消息
	     chat.messages.push({
	       id: chat.msgListKey ,  
	       timestamp: newMsg.timestamp,
	       show: true,               // 固定为true
	       msg: newMsg.content,
	       status: 1
	     })
	     
	     // 更新msgListKey
	     chat.msgListKey -= 1
		 
		 return chatList
	   },
			    
	   // 创建新聊天
	   createNewChat(chatList,anonymousKey,addr,newMsg,sign) {
	   	console.log("正在创建",sign)
	   	let roleType=1
	      if(newMsg.auth==="1"){
	      	  roleType=1
	      }else if(newMsg.auth==="2"){
	      		roleType=2
	      }else if(newMsg.auth==="3"){
	   	roleType=3
	     }else{
	   	roleType=0
	     }
	     
	    let sender=newMsg.sender;
	    if("ANONYMOUS_MSG"===sign){
	    	sender= 'anonymous_'+newMsg.sender
	    }
	    chatList.push({
	       id: sender,
	   	readed:true,
	   	roleType:roleType,
	       name:  `匿名用户_${anonymousKey}_${Date.now().toString(36).substr(0, 4)}`,
	       avatar:addr+newMsg.avatar_path,
	       msgListKey: -1,  // 初始值
	       messages: [{
	         id: 0,        // msgListKey-1
	         timestamp: newMsg.timestamp,
	         show: true,
	         msg: newMsg.content,
	         status: 1
	       }]
	     })
	     //anonymousKey+1;
		 return {
			 "chatList":chatList,
			 "anonymousKey":anonymousKey+1
		 }
	   },
	   saveAnonymousCount(newCount,userId) {
	     uni.setStorageSync('anonymousKey'+userId, newCount)
	   },
};

export default {util}; 